package com.project.service;

import com.project.common.utils.DeleteFileUtil;
import com.project.common.utils.Util;
import com.project.dao.CvMapper;
import com.project.entity.Cv;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/26 13:36
 * @param:
 * @return:
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class CvServiceImpl extends BaseService implements CvService{
    @Autowired
    private CvMapper cvMapper;
    /**
     * author: WheelChen
     * date: 2017/9/4
     * function: 添加一个简历
     *
     * @param cv
     * @return
     */
    @Override
    public int addOne(Cv cv) {
        return cvMapper.insertSelective(cv);
    }

    /**
     * author: WheelChen
     * date: 2017/9/4
     * function: 删除一个简历
     *
     * @param id
     * @return
     */
    @Override
    public boolean removeOneById(Integer id) {
        boolean flag = false;
        //简历的基本目录的绝对地址
        Cv cv = findOneById(id);

        String rootPath = Util.getWebRootURL(this);

        String realPath = rootPath + cv.getFilename();

        if (cvMapper.deleteByPrimaryKey(id) > 0 && DeleteFileUtil.deleteFile(realPath)) flag = true;
        return flag;
    }

    /**
     * author: WheelChen
     * date: 2017/9/4
     * function: 修改一个简历
     *
     * @param cv
     * @return
     */
    @Override
    public int modifyOneById(Cv cv) {
        return cvMapper.updateByPrimaryKeySelective(cv);
    }

    /**
     * author: WheelChen
     * date: 2017/9/5
     * function:
     *
     * @param id
     * @return
     */
    @Override
    public Cv findOneById(Integer id) {
        return cvMapper.selectByPrimaryKey(id);
    }

    /**
     * author: WheelChen
     * date: 2017/9/7
     * function: 根据条件模糊查找简历
     *
     * @param cv
     * @return
     */
    @Override
    public List<Cv> findByCondition(Cv cv) {
        return cvMapper.selectByCondition(cv);
    }
    /**
     * @param stid
     * @return
     */
    @Override
    public List<Cv> findBatchByStid(Integer stid) {
        return cvMapper.selectBatchByStid(stid);
    }

    /**
     * author: WheelChen
     * date: 2017/10/22
     * function: 根据用户表idList批量查找简历
     *
     * @param stidList 用户表idList
     * @return
     */
    @Override
    public List<Cv> findBatchByStidList(List stidList) {
        return cvMapper.selectBatchByStidList(stidList);
    }

    /**
     * author: WheelChen
     * date: 2017/10/22
     * function: 根据用户表id批量删除简历
     *
     * @param stid 用户表idList
     * @return
     */
    public boolean removeBatchByStid(List stid) {
        List<Cv> cvList = cvMapper.selectBatchByStidList(stid);
        for (Cv cv : cvList){
            if (!removeOneById(cv.getId())) return false;
        }
        return true;
    }
}
